<?php
namespace Home\Controller;
use Think\Controller;
class ActivityController extends BackController {
    public function index(){
				//校区
				if((session('qe_user')['xid']!=0) and (session('qe_user')['pwdid']==7)){
					$map['xid'] = array('eq',session('qe_user')['xid'],'and');
				}
        //名称
        $result['name'] = I('name');
        if ($result['name']){
            $map['a.mark'] = array('like','%'.$result['name'].'%');
        }
        //收支类型
        $result['curlist'] = I('curlist');
        if ($result['curlist']){
            $map['a.bid'] = array('eq',$result['curlist']);
        }
        //账目类型
        $result['status'] = I('status');
        if ($result['status']){
            $map['a.status'] = array('eq',$result['status']);
        }
        //查找所有课程
        $user = session('qe_user');
        if ($user['xid'] <=0 || empty($user['xid'])){
            $this->error('该用户不是校长');die;
        }
        $where['xid'] = $user['xid'];
        $curlist = M('curriculum')->where()->select();
        $status = array(
            1=>'启用',
            2=>"不启用"
        );
        $sc_student = M('activity');
        $count      = $sc_student
            ->alias('a')
            ->join('left join curriculum as c on c.id=a.bid')
            ->where($map)
            ->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $sc_data = $sc_student
            ->alias('a')
            ->field('a.*,c.cname')
            ->join('left join curriculum as c on c.id=a.bid')
            ->where($map)
            ->order('a.id desc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();

        $this->assign('sc_data',$sc_data);
        $this->assign('result',$result);
        $this->assign('curlist',$curlist);
        $this->assign('status',$status);
        $this->assign('page',$show);
        //var_dump($where);
        $this->display();
    }

    public function add(){
        $result = I();
        //获取当前校长的id
        if ($result['id'] != ''){
            $map['id'] = $result['id'];
            $find = M('activity')->where($map)->find();
            $this->assign('map',$find);
            if (empty($find)){
                $this->error('操作错误');die;
            }
        }
        $user = session('qe_user');
        if ($user['xid'] <=0 || empty($user['xid'])){
            $this->error('该用户不是校长');die;
        }
        $where['xid'] = $user['xid'];
        $curlist = M('curriculum')->where()->select();
        $status = array(
          1=>'启用',
          2=>"不启用"
        );
        $this->assign('curlist',$curlist);
        $this->assign('status',$status);
        $this->display();
    }
    //新曾优惠
    public function addsave(){
        $result = I();
        if ($result['name'] == ''){
            $this->error('活动名不能为空');die;
        }
        if ($result['stardate'] == ''){
            $this->error('活动开始时间不能为空');die;
        }
        if ($result['stopdate'] == ''){
            $this->error('活动结束时间不能为空');die;
        }
        if ($result['money'] == ''){
            $this->error('活动优惠金额不能为空');die;
        }
        $map['name'] = $result['name'];
        $map['bid'] = $result['kid'];
        $map['startime'] = $result['stardate'];
        $map['stoptime'] = $result['stopdate'];
        $map['amount'] = $result['money'];
        $map['mark'] = $result['mark'];
        $map['status'] = $result['status'];
        if ($result['id'] > 0){
            $where['id'] = $result['id'];
            $data = M('activity')->where($where)->save($map);
        }else{
            $data = M('activity')->add($map);
        }
        if ($data){
            $this->success('添加成功',U('Activity/index'));
        }else{
            $this->error('添加失败');die;
        }
    }
    //删除优惠
    public function del(){
        $map['id'] = I('id');
        $data = M('activity')->where($map)->delete();
        if ($data){
            $this->success('操作成功！',U('Activity/index'));
        }else{
            $this->error('操作失败');die;
        }
    }

}